package com.fingard.dsp.web.page.m;

import java.sql.Connection;
import java.util.HashMap;

import com.fingard.FGBiz;
import com.fingard.common.utils.json.JSONUtils;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.web.page.PageBase;

public class DoExec extends PageBase {
	@Override
    public void pageLoad() throws Exception {
        httpContext.response.isRtnBytes = false;
        httpContext.response.setContentType("txt", "");
        StringBuilder sbJson = new StringBuilder();
        String respCode = "";
        String respInfo = "";
        Connection conn = null;
        try {
            String execText = "";
            String yearMonth = "";
            String reqJson = httpContext.request.httpReceiver.getReqBodyContent();
            HashMap<String, String> reqParams = JSONUtils.json2Map(reqJson);
            
            if (reqParams.containsKey("execText")) {
            	execText = reqParams.get("execText");
            }
            if (reqParams.containsKey("yearMonth")) {
            	yearMonth = reqParams.get("yearMonth");
            }

            if (execText.length() > 0 && yearMonth.length()>0) {
            	Object[] objs = null;
            	
            	conn = FGBiz.partTable.getLoaclDbConnByPart("", Format.Date8Format.parse(yearMonth+"01"));
            	int rowAffs = JDBCHelper.update(conn, execText, objs);
            	respCode = "0";
            	respInfo = "执行成功，影响行数："+rowAffs;
            }else if(execText.length() == 0){
            	respCode = "-1";
            	respInfo = "执行语句为空；";
            }else if(yearMonth.length() == 0){
            	respCode = "-1";
            	respInfo += "月份为空；";
            }
            
        } catch (Exception ex) {
            respCode = "-1";
            respInfo = LogHelper.getStackTraceFirstLine(ex);
            FGBiz.limitMsg.exception(this.getClass().getSimpleName(), ex);
        } finally {
        	try{
        		if (conn != null) {
                    conn.close();
                }
        	}catch(Exception e){
        		FGBiz.limitMsg.exception(this.getClass().getSimpleName(), e);
        	}
            
        }

        sbJson.append("{");
        sbJson.append("\"respCode\":" + "\"" + respCode + "\",");
        sbJson.append("\"respInfo\":" + "\"" + respInfo + "\"");
        sbJson.append("}");
        httpContext.response.respMsg = sbJson.toString();
    }
}
